const state = {
  token: sessionStorage.getItem('token') || '',
  userInfo: JSON.parse(sessionStorage.getItem('userInfo')) || {}
}

const mutations = {
  // 保存token
  setToken (state, token) {
    sessionStorage.setItem('token', token)
    state.token = token
  },
  // 保存用户登录信息
  setUserInfo (state, userInfo) {
    sessionStorage.setItem('userInfo', JSON.stringify(userInfo))
    state.userInfo = userInfo
  }
}

const actions = {
  updateToken (context, value) {
    context.commit('setToken', value)
  },
  updateUserInfo (context, value) {
    context.commit('setUserInfo', value)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
